package com.quyunshuo.module.home.fragment.project

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.quyunshuo.module.home.R
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleBean

// RecyclerView Adapter
class ProjectAdapter(private val data: List<ArticleBean>) :
    RecyclerView.Adapter<ProjectAdapter.ViewHolder>() {
    private val TAG = "ProjectAdapter"
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.rv_item_project, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val itemHeight = (200..600).random()
        holder.ivImg.layoutParams.height = itemHeight
        holder.ivImg.load(data[position].envelopePic)
        Log.d(TAG, "onBindViewHolder: " + data[position])
        holder.tvText.text = data[position].title
    }

    override fun getItemCount() = data.size

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivImg: ImageView = itemView.findViewById(R.id.iv_img)
        val tvText: TextView = itemView.findViewById(R.id.tv_text)

    }
}
